﻿#pragma strict
#pragma implicit
#pragma downcast
import System.Collections.Generic;
//////////////////////////////////////////////////////
///场景,组件,导出工具.
//////////////////////////////////////////////////////
/** 上次场景保存的名字 */
private static var lastSceneName:String = "";
/** 常量 */
private static var strSaveScene:String = "Where"; //指定保存的位置.
private static var lastDirPath:String = "";
private static var strLastDirPath:String = "lastDirPath";
private static var strDefaultLastDirPath:String = "";
/** 导出的场景后缀 */
private static var strUnity3d:String = "unity3d";
/** 场景后缀 */
private static var strSceneExtendName:String = "unity";
private static var strSceneExportError:String = "no found scene file!";
private static var strExport:String = "Export"; 
private static var strChoosePrefab:String = "Choose Prefab in Project";

/** 导出场景 */
@MenuItem("Level4/Export/ExportScene") //导出场景
static function BuildStreaedScene(){
	var pathList:List.<String> = new List.<String>();
	var objs:Object[] = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
	for(var obj:UnityEngine.Object in objs){
		var path:String = AssetDatabase.GetAssetPath(obj);
		if(path.Contains(strSceneExtendName)){
			pathList.Add(path);
		}
	}
	
	if(pathList.Count > 0){
		lastDirPath = PlayerPrefs.GetString(strLastDirPath,strDefaultLastDirPath);
		path = EditorUtility.SaveFilePanel(strSaveScene,lastDirPath,lastSceneName,strUnity3d);
		if(!String.IsNullOrEmpty(path)){
			PlayerPrefs.SetString(strLastDirPath,System.IO.Directory.GetParent(path).ToString());
			BuildPipeline.BuildStreamedSceneAssetBundle(pathList.ToArray(),path,GetBuildTarget());
			OpenDir(path);
		}
	}else{
		Debug.LogError(strSceneExportError);
	}
	pathList.Clear();
	pathList = null;
}
private static function GetBuildTarget():BuildTarget{
	if(EditorUtility.DisplayDialog("Warning","pc or web?","pc","web")){
		return BuildTarget.StandaloneWindows;
	}
		return BuildTarget.WebPlayer;
}
/**
	导出素材
*/
@MenuItem("Level4/Export/ExportSinglePrefab") //导出单个资源
static function ExportResource(){
	if(Selection.activeObject){
		var srcName:String = Selection.activeObject.name;
		var path:String = EditorUtility.SaveFilePanel(strExport,"",srcName,strUnity3d);
		if(path.Length != 0){
			BuildPipeline.BuildAssetBundle(Selection.activeObject,Selection.objects,path);
		}
	}else{
		Debug.Log(strChoosePrefab); //选中要导出的物体
	}
}
/**
	导出素材和素材依赖
*/
@MenuItem("Level4/Export/ExportMultiPrefabsInOne") //多个资源导出为1个
static function ExportResourceTrack(){
	if(Selection.activeObject){
		var srcName:String = Selection.activeObject.name;
		lastDirPath = PlayerPrefs.GetString(strLastDirPath,strDefaultLastDirPath);
		var path:String = EditorUtility.SaveFilePanel(strExport,lastDirPath,srcName,strUnity3d);
		if(path){
			PlayerPrefs.SetString(strLastDirPath,System.IO.Directory.GetParent(path).ToString());
			
			var objs:Object[] = Selection.GetFiltered(Object,SelectionMode.DeepAssets);
			BuildPipeline.BuildAssetBundle(Selection.activeObject,objs,path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
			Selection.objects = objs;
			//打开导出的目录
			OpenDir(path);
		}
	}else{
		Debug.Log(strChoosePrefab);
	}
}

@MenuItem("Level4/Export/ExportMultiPrefabsOneByOne") //逐个导出多个资源
static function ExportResourcesTrack(){
	var objs:UnityEngine.Object[] = Selection.objects;
	var path:String = null;
	for(var obj:UnityEngine.Object in objs){
		var srcName:String = obj.name;
		lastDirPath = PlayerPrefs.GetString(strLastDirPath,strDefaultLastDirPath);
		path = EditorUtility.SaveFilePanel(strExport,lastDirPath,srcName,strUnity3d);
		if(path){
			PlayerPrefs.SetString(strLastDirPath,System.IO.Directory.GetParent(path).ToString());
			BuildPipeline.BuildAssetBundle(obj,null,path,BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets);
			Selection.activeObject = obj;
		}else{
			//全部取消.
			break;
		}
	}
	if(path){
		OpenDir(path);
	}
}

/**
	打开目录
*/
static function OpenDir(path:String){
	if(EditorUtility.DisplayDialog("Warning","Open exported directory?","yes","no")){ //"注意","打开刚才导出文件的目录?","是","否"
		path = System.IO.Directory.GetParent(path).ToString();
		System.Diagnostics.Process.Start(path);
	}
}
